<template>
    <div class="view-wrapper">
        <div class="head">
            <el-form :inline="true" :model="formInline" class="demo-form-inline">
                <el-form-item label="">
                    <el-input v-model="formInline.query" placeholder="商品名称/商品id" style="width: 300px"></el-input>
                </el-form-item>
                <el-form-item label="商户名称：">
                    <el-select v-model="optionsId" placeholder="输入查询勾选，可多选" @change="merchantChange" multiple
                        filterable style="width: 300px">
                        <el-option v-for="item in options" :key="item.id" :label="item.value" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="状态：">
                    <el-select v-model="formInline.status" placeholder="请选择">
                        <el-option v-for="item in optionsStatus" :key="item.id" :label="item.value" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="审核状态：">
                    <el-select v-model="formInline.auditStatus" placeholder="请选择">
                        <el-option v-for="item in optionsAudit" :key="item.id" :label="item.value" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="售卖时间：">
                    <el-select v-model="time" placeholder="请选择" @change="changeTime">
                        <el-option v-for="item in timeOptions" :key="item.id" :label="item.value" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="" v-if="timeDisabled">
                    <el-date-picker v-model="date" start-placeholder="开始日期" end-placeholder="结束日期" :clearable="false"
                        type="datetimerange" format="yyyy-MM-dd HH:mm" value-format="yyyy-MM-dd HH:mm"
                        @change="handleTime">
                    </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="search">查询</el-button>
                    <el-button @click="reset">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="listBox">
            <div class="listBox_head">
                <el-button type="primary" class="sumit_button" @click="addBuy">添加团购商品</el-button>
                <!-- <el-button type="primary" class="sumit_button" @click="addBuy">同步商品列表</el-button> -->
            </div>
            <ex-table ref="table" :loading="loading" :table-column="tableColumn" :table-data="tableData"
                @pageSizeChange="handleSizeChange" @currentPageChange="handleCurrentChange">
                <template v-slot:operate="{ scope }">
                    <el-button type="text" size="mini" @click="watchClick(scope.row)">查看</el-button>
                    <el-button type="text" size="mini" @click="editClick(scope.row)" v-if="scope.row.auditStatus!=1">编辑</el-button>
                    <el-button type="text" size="mini" @click="salesUpDate(scope.row)"
                        v-if="scope.row.status == '2'">开始售卖</el-button>
                    <el-button type="text" size="mini" @click="salesUpDate(scope.row)"
                        v-if="scope.row.status == '1'">结束售卖</el-button>
                    <el-button type="text" size="mini" @click="copyAddClick(scope.row)">复制并创建</el-button>
                </template>
            </ex-table>
        </div>
        <EditDialog :id="dialogId" v-bind="$attrs" v-if="dialogVisible" :visible.sync="dialogVisible"
            @success="_handleSuccess" />

    </div>
</template>
<script>
// components
import {
    adminDict,
    getAnchorTradeGoodsList,
    stopGroupon,
    getAnchorTradeMerchant,
    updateAnchorTrade
} from "@/api/operateActive";
import EditDialog from "@/views/consumeMiningManagement/dyGroupBuying/components/editDialog";
import ExportExcelButton from "@/components/ExportExcelButton/index";
// import DetailDialog from "./components/DetailDialog";
import handlePageMixin from "@/mixins/handlePageMixin";
import { options } from "dropzone";
import {
    mapGetters
} from "vuex";
export default {
    name: "billList",
    components: {
        ExportExcelButton,
        EditDialog,
        // DetailDialog,
    },
    mixins: [handlePageMixin],
    data() {
        return {
            rowKey: 'id',
            loading: false,
            dialogId: '', //审核id
            formInline: {
                query: "",
                dyShopAccountId: "",
                soldEndTime: "",
                soldStartTime: "",
                auditStatus: "",
                status: "",
            },
            time: 0,
            date: [],
            timeDisabled: true,
            dialogVisible: false,
            addCount: "", //增发数量
            options: [],
            optionsId: [],//商户查询id列表
            optionsStatus: [],
            optionsAudit: [],
            timeOptions: [
                { id: 0, value: '自定义' }, { id: 1, value: '不限时间' }
            ],
            tableData: [],
            tableColumn: [
                {
                    prop: "dyAccountName",
                    label: "商户名称",
                    width: 180
                },
                {
                    prop: "productId",
                    label: "商品id"
                },
                {
                    prop: "productName",
                    label: "商品名称",
                    width: 180
                },
                {
                    prop: "actualAmount",
                    label: "售价",
                },
                {
                    prop: "activeTime",
                    label: "售卖时间",
                    width: 180
                },
                {
                    prop: "stock",
                    label: "已售券数/库存",
                    width: 150
                },
                {
                    prop: "statusDict",
                    label: "状态",
                    width: 120
                },
                {
                    prop: "auditDict",
                    label: "审核状态",
                    width: 120
                },
                {
                    label: "操作",
                    slot: true,
                    slotName: "operate",
                    width: 240,
                    fixed: "right",
                },
            ],
        };
    },
    computed: {
        ...mapGetters(["isChain"]),
    },
    created() { },
    mounted() {
        this.getlinkagelist();
        this.getList();
        this.getselectList();
    },
    methods: {
        getselectList() {
            //获取商家列表
            this.loading = true;
            getAnchorTradeMerchant()
                .then((res) => {
                    this.options = res.result;
                })
                .finally(() => {
                    this.loading = false;
                });
        },
        handleTime(e) {
            this.formInline.soldStartTime = this.date[0]
            this.formInline.soldEndTime = this.date[1]
        },
        changeTime(e) {
            if (e == 1) {
                this.timeDisabled = false
                this.formInline.soldStartTime = ""
                this.formInline.soldEndTime = ""
            }
            if (e == 0) {
                this.timeDisabled = true
                this.formInline.soldStartTime = this.date[0]
                this.formInline.soldEndTime = this.date[1]
            }
        },
        // 商户查询改变
        merchantChange(e) {
            if (this.optionsId.length > 0) {
                this.formInline.dyShopAccountId = this.optionsId.toString()
            } else {
                this.formInline.dyShopAccountId = ''
            }
        },
        //获取字典
        getlinkagelist() {
            const params = {
                dictType: "anchor_trade_goods_status,anchor_trade_goods_audit_status",
            };
            adminDict(params).then((res) => {
                var arr = [{
                    id: "",
                    value: "全部",
                },];
                this.optionsStatus = arr.concat(res.result[0].dictList);
                this.optionsAudit = arr.concat(res.result[1].dictList);
            });
        },
        _handleSuccess(e) {
            //审核成功回调
            this.reset()
        },
        addBuy() {
            //新增团购
            this.$router.push({
                path: "/addCommodity",
            });
        },
        //查看
        watchClick(row){
            this.$router.push({
                path: "/watchCommodity?id=" + row.id,
            });
        },
        // 编辑
        editClick(row) {
            //已上架商品点击进行免审信息编
            if (row.auditStatus == '2' && row.status == '1') {
                this.dialogId = row.id
                this.dialogVisible = true;
            } else {
                //已下架商品点击直接进入编辑页面编辑更多信息：回显全部商品信息重新编辑重新审核
                this.$router.push({
                    path: "/addCommodity?id=" + row.id,
                });
            }
        },
        //复制并创建
        copyAddClick(row){
            this.$router.push({
                path:'/addCommodity',
                query: {
                    id: row.id,
                    type: 'copy',
                }
            })
        },
        //售卖
        salesUpDate(row) {
            let title = ''
            let status = ''
            if (row.status == 2) {
                title = '开始售卖'
                status = '1'
            } else {
                title = '结束售卖'
                status = '2'
            }
            this.$confirm('确定' + title + '此信息?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var params = {
                    id: row.id,
                    status: status,
                };
                updateAnchorTrade(params).then(response => {
                    this.$message({
                        type: 'success',
                        message: '操作成功!'
                    });
                    this.getList();
                });

            }).catch(() => { });
        },
        deactivate(id) {
            //停用
            this.$confirm("是否停用？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    stopGroupon(id).then((res) => {
                        this.$message({
                            message: "停用成功",
                            type: "success",
                            duration: 1500,
                        });
                        setTimeout(() => {
                            this.reset();
                        }, 800);
                    });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消",
                    });
                });
        },
        search() {
            //搜索
            console.log(this.formInline);
            this.pageOptions.pageNum = 1;
            this.$refs.table.pageOptions.pageNum = 1;
            this.getList();
        },
        reset() {
            this.time = 0;
            this.timeDisabled = true;
            this.optionsId = []
            this.formInline = {
                query: "",
                dyShopAccountId: "",
                soldEndTime: "",
                soldStartTime: "",
                auditStatus: "",
                status: "",
            };
            this.pageOptions.pageNum = 1;
            this.$refs.table.pageOptions.pageNum = 1;
            this.getList();
        },
        getList() {
            console.log('9999', this.formInline.soldStartTime)
            const params = {
                ...this.pageOptions,
                ...this.formInline,
            };
            console.log('9999', params)
            this.loading = true;
            getAnchorTradeGoodsList(params)
                .then((res) => {
                    const {
                        total,
                        records
                    } = res.result;
                    records.forEach((item) => {
                        if (item.soldTimeType == '1') {
                            item.activeTime = item.soldStartTime + ' - ' + item.soldEndTime
                        } else {
                            item.activeTime = "不限时间"
                        }
                        if (item.stockLimitType == '1') {
                            item.stock = item.stockSoldCount + '/' + item.stockCount
                        } else {
                            item.stock = item.stockSoldCount + '/不限库存'
                        }
                        item.actualAmount = '￥' + item.actualAmount
                        if (item.productId == '') {
                            item.productId = "-"
                        }
                        if (item.status == '1') {
                            item.statusDict = "已上架"
                        } else if (item.status == '2') {
                            item.statusDict = "已下架"
                        } else if (item.status == '3') {
                            item.statusDict = "禁售"
                        } else {
                            item.statusDict = "-"
                        }
                        if (item.auditStatus == '1') {
                            item.auditDict = "审核中"
                        } else if (item.auditStatus == '2') {
                            item.auditDict = "审核通过"
                        } else if (item.auditStatus == '3') {
                            item.auditDict = "审核失败"
                        } else if (item.auditStatus == '4') {
                            item.auditDict = "草稿"
                        } else {
                            item.auditDict = "-"
                        }
                    });
                    this.tableData = records;
                    this.$refs.table.pageOptions.total = total;
                })
                .finally(() => {
                    this.loading = false;
                });
        },
    },
};

</script>
<style lang="scss" scoped>
.el-card-title {
    font-size: 20px;
}

.head {
    //   box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    //   height: 120px;
    padding: 20px 20px 0;
}

.el-form--inline .el-form-item {
    margin-right: 10px;
}

.listBox {
    //   box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    //   height: 120px;
    padding: 20px 20px;
    margin-top: 10px;
}

.table_list {
    display: flex;
    align-items: center;
    // border: 1px solid #DCDFE6;
    border-radius: 7px;
    width: 220px;
    margin-bottom: 10px;
}

.tableBox {
    width: calc(100% / 3);
    height: 36px;
    text-align: center;
    line-height: 36px;
    color: #303133;
    font-size: 14px;
    border: 1px solid #dcdfe6;
    border-right: 0;
}

.tableBox:first-child {
    border-radius: 7px 0px 0px 7px;
}

.tableBox:last-child {
    border-right: 1px solid #dcdfe6;
    border-radius: 0px 7px 7px 0px;
}

.spchoosetext {
    background-color: #fff4e6;
    color: #ff9300;
    // border: 1px solid #ff9300;
}

.listBox_head {
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
}

.slot_box {
    display: flex;
    align-items: center;
}

.avatar_img {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    margin-right: 10px;
}

// .sumit_button {
//   color: #fff !important;
//   background: #ff7b35 !important;
//   border: 0;
// }

// .sumit_button:hover {
//   opacity: 0.9;
// }

.card_box {
    width: 160px;
    height: 100px;
    background: linear-gradient(112deg,
            #f85936 0%,
            rgba(255, 147, 123, 0.99) 100%);
    border-radius: 4px;
    font-size: 16px;
    font-weight: bold;
    color: #fff;
    text-align: center;
    line-height: 100px;
}
</style>